/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>HHotelServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年9月12日 下午3:50:38<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HAddressMapper;
import com.flea.hclv.model.dao.HHotelMapper;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HHotel;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.service.HHotelService;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description
 * @createTime 2018年9月12日 下午3:50:38
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Service("hotelService")
public class HHotelServiceImpl implements HHotelService {
	@Autowired
	private HHotelMapper hotelMapper;
	@Autowired
	private HAddressMapper addressMapper;
	
	@Autowired
	private HResourceRelationMapper hResourceRelationMapper;

	@Override
	public boolean saveOrUpdate(HHotel entity, HAddress addressEntity,
			String userId) {
		String id = entity.getId();
		entity.setModifyUser(userId);
		addressEntity.setModifyUser(userId);
		String addressId = FleaStringUtils.getGenerateUUID();
		int result = 0;
		int addResult = 0;
		if (StringUtil.isEmpty(id)) {
			id = FleaStringUtils.getGenerateUUID();
			entity.setId(id);
			entity.setCreateUser(userId);
			OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
			String deptId = HConstant.HCLV_ADMIN_ADMIN;

			if (null != dept) {

				deptId = dept.getDeptUuid();
			}
			entity.setUuid(deptId);

			addressEntity.setId(addressId);
			addressEntity.setCreateUser(userId);
			addressEntity.setUuid(id);

			result = addressMapper.insertSelective(addressEntity);

			result = hotelMapper.insertSelective(entity);

		} else {
			addressEntity.setUuid(id);
			result = hotelMapper.updateByPrimaryKeySelective(entity);

			addResult = addressMapper
					.updateByPrimaryKeySelective(addressEntity);
		}

		return result > 0 && addResult > 0;
	}

	@Override
	public FleaPage<HHotel> findForGird(String queryName, String queryLevel, String queryProvince, String queryCity, String queryZone,int curPage, int pageSize) {
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		

		List<HAddress> address = addressMapper.selectByParams(queryProvince,queryCity,queryZone);
		
		List<HHotel> pageData = hotelMapper.selectPagingsByParams(rowBounds,queryName,queryLevel,address);
		FleaPage<HHotel> page = new FleaPage<HHotel>(pageData);
		return page;
	}


	@Override
	public boolean deleteByIds(String ids, String userId) {


		List<String>  idsArray = JSON.parseArray(ids, String.class);
		addressMapper.deleteByUuids(idsArray);
		int result = hotelMapper.deleteByPrimaryKeys(idsArray);
		return result == idsArray.size() && result > 0;
	}

	@Override
	public List<HHotel> queryByOrgId(String orgId) {
	
	   List<HHotel> result = hotelMapper.selectlAllByUuid(orgId);
		
		
		return result;
	}

	@Override
	public List<HHotel> queryAll() {
		 List<HHotel> result = hotelMapper.selectlAll();
		return result;
	}

	@Override
	public boolean saveOrUpdate(HHotel entity, HAddress addressEntity, String userId,
			List<HResourceRelation> filesRelation) {

		String id = entity.getId();
		entity.setModifyUser(userId);
		addressEntity.setModifyUser(userId);
		String addressId = FleaStringUtils.getGenerateUUID();
		int result = 0;
		int addResult = 0;
		if (StringUtil.isEmpty(id)) {
			id = FleaStringUtils.getGenerateUUID();
			entity.setId(id);
			entity.setCreateUser(userId);
			OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
			String deptId = HConstant.HCLV_ADMIN_ADMIN;

			if (null != dept) {

				deptId = dept.getDeptUuid();
			}
			entity.setUuid(deptId);

			addressEntity.setId(addressId);
			addressEntity.setCreateUser(userId);
			addressEntity.setUuid(id);

			result = addressMapper.insertSelective(addressEntity);

			result = hotelMapper.insertSelective(entity);

		} else {
			addressEntity.setUuid(id);
			addressEntity.setId(addressEntity.getAddRessId());
			addResult = addressMapper
					.updateByPrimaryKeySelective(addressEntity);
			result = hotelMapper.updateByPrimaryKeySelective(entity);

			
		}
		
		int fileSize = null==filesRelation?0:filesRelation.size();
		if (result > 0  && fileSize > 0) {
			int resourceCount = 0;
			
			for (HResourceRelation hResourceRelation : filesRelation) {
				String hResourceRelationId = hResourceRelation.getId();
				hResourceRelation.setModifyUser(userId);
				if(StringUtil.isEmpty(hResourceRelationId)){
					hResourceRelationId = FleaStringUtils.getGenerateUUID();
					hResourceRelation.setId(hResourceRelationId);
					hResourceRelation.setCreateUser(userId);
					hResourceRelation.setUuid(id);
					resourceCount +=	hResourceRelationMapper.insertSelective(hResourceRelation);
				}else{
					//只有删除和添加，并没有修改
					resourceCount += hResourceRelationMapper.updateByPrimaryKeySelective(hResourceRelation);
				}
			}
			return resourceCount==fileSize && resourceCount >0;
		}else{
			return result > 0 && addResult > 0;
		}
	}

	@Override
	public FleaPage<HHotel> findForGirdBySelf(HttpServletRequest request,String queryName,
			String queryLevel, String queryProvince, String queryCity,
			String queryZone, int curPage, int pageSize) {
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		String deptOrgId = FleaOrgUtils.getCurrentUserDeptUuid(request);

		List<HAddress> address = addressMapper.selectByParams(queryProvince,queryCity,queryZone);
		
		List<HHotel> pageData = hotelMapper.selectPagingsByParamsAndOrgId(rowBounds,deptOrgId,queryName,queryLevel,address);
		FleaPage<HHotel> page = new FleaPage<HHotel>(pageData);
		return page;
	}


}
